<?php
/**
 * Created by PhpStorm.
 * User: CaiJianling
 * Date: 2018/6/19
 * Time: 20:22
 */
require 'core/header.php';
if (!isset($_GET['topicid'])) {
    header('Location:forum.php');
}
if (!isset($_GET['p'])) {
    $p = 1;
} else {
    $p = $_GET['p'];
}

if (isset($_SESSION['uid'])) {
    $user_all = sql_search_uid_all($_SESSION['uid']);
}
$offset = ($p - 1) * 10;//pagesize = 10
$gettopicid = $_GET['topicid'];
$topics = sql_topicid_topics($gettopicid);

//查出发帖用户信息：
//uid:
$posteruid = $topics['topic_poster'];
//根据uid查出所有信息
$posters = sql_search_uid_all($posteruid);

//查出登陆用户信息
if (isset($_SESSION['uid'])) {
    $loginers = sql_search_uid_all($_SESSION['uid']);
}
//查出forum auth_edit
$topic_forumid = $topics['topic_forumid'];
$sql111 = "SELECT auth_edit FROM bbs0_forums WHERE forum_id = '$topic_forumid'";
$auth_edit_array = mysqli_fetch_array(mysqli_query($conn, $sql111));
$auth_edit = $auth_edit_array['auth_edit'];


$topicid = $_GET['topicid'];
$forums = sql_topicid_forums($topicid);
$fid = $forums['forum_id'];
?>
    <hr>
    <a href="forums.php?forumid=<?= $topics['topic_forumid'] ?>&p=1"">返回列表</a>
    <br/>
<?php
switch ($topics['topic_status']) {
    case "0":
        if (!isset($_SESSION['rank'])) {
            echo "<script>alert('非法访问');history.back();</script>";
            exit();
        } elseif ($_SESSION['uid'] == $posteruid || $_SESSION['rank'] == $auth_edit) {
            break;
        } elseif ($_SESSION['rank'] < 10) {
            echo "<script>alert('此贴已隐藏');history.back();</script>";
            exit();
        }
    case "2":
        if (!isset($_SESSION['rank'])) {
            echo "<script>alert('非法访问');history.back();</script>";
            exit();
        } elseif ($_SESSION['uid'] == $posteruid || $_SESSION['rank'] == $auth_edit) {
            break;
        } elseif ($_SESSION['rank'] < $auth_edit) {
            echo "<script>alert('此贴待审核');history.back();</script>";
            exit();
        }
}

$count_result = sql_topic_page($gettopicid);
$count_array = mysqli_fetch_array($count_result);
$pagenum = ceil($count_array['COUNT(*)'] / 10);


echo '<p style="text-align: right">';
if ($p > 1) {
    echo '<a href="topic.php?topicid=' . $gettopicid . '&p=' . ($p - 1) . '"><<上一页</a>&nbsp;&nbsp;';
}
echo '共 ', $count_array['COUNT(*)'], ' 条';
if ($pagenum > 1) {
    for ($i = 1; $i <= $pagenum; $i++) {
        if ($i == $p) {
            echo '&nbsp;[', $i, ']';
        } else {
            echo '&nbsp;<a href="topic.php?topicid=' . $gettopicid . '&p=' . $i . '">' . $i . '</a>';
        }
    }
}
if ($p < $pagenum) {
    echo '&nbsp;&nbsp;<a href="topic.php?topicid=' . $gettopicid . '&p=' . ($p + 1) . '">下一页>></a>';
}
echo '</p>';


?>
    <table border="1" width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <th width="20%">发帖用户信息</th>
            <th width="80%">
                <?php
                if ($topics['topic_top'] == 1) {
                    echo '[<span style="color:#FF6600">置顶</span>]';
                }
                $type_val = sql_topicid_type($_GET['topicid']);
                if ($type_val == 1) {
                    $type_url = "<a href='forums.php?forumid=" . $topic_forumid . "&type=1&p=1'><span style=\"color: #FF0000\">求助</span></a>";
                } elseif ($type_val == 2) {
                    $type_url = "<a href='forums.php?forumid=" . $topic_forumid . "&type=2&p=1'><span style=\"color: #00CC00\">已解决</span></a>";
                }else
	                $type_url = "未知";
                ?>
                [<?= $type_url ?>]
                <span style="color: #FF0000">主题：</span><a
                        href="topic.php?topicid=<?= $gettopicid ?>&p=1"><?= $topics['topic_title'] ?></a></th>
        </tr>
        <?php
        if ($p == 1) {
            /**
             * 帖子信息
             */
            echo '<tr>';
            //发帖用户
            echo '<td><center>';
            echo '<a href="home.php?mod=space&uid=' . $posters['user_uid'] . '" target="_blank">' . $posters['user_username'] . '</a>';
            echo '<br />';
            echo '<img src="' . $posters['user_himg'] . '"><br />';
            echo '等级：' . $posters['user_level'] . '<br />';
            $result = sql_search_medals($posters['user_uid']);
            while (($result_array = mysqli_fetch_array($result)) != null) {
                $narray = sql_medalid_medals($result_array['medal_id']);
                echo '<img src="' . $narray['medal_url'] . '" width="50px"
                height="50px" title="' . $narray['medal_name'] . '"/>';
            }
            echo '<br />用户组：' . $user_rankname = sql_search_rank_rankname($posters['user_rank']);
            echo '</td></center>';
            //帖子主题内容

            echo '<td>';
            //查询主题内容
            $topic_topics = sql_topicid_posts($_GET['topicid'], 0);
            echo '<p>发表于：' . date("Y年m月d日 H:i:s", $topic_topics['post_createtime']) . '&nbsp;<font color="#ff0000">楼主</font></p>';
            if ($topic_topics['post_status'] == 1) {
                $query = sql_key_query();
                $replycontent = $topic_topics['post_content'];
                while (($array = mysqli_fetch_array($query)) != false) {
                    $replycontent = preg_replace('/' . $array['key_name'] . '/', $array['key_change'], $replycontent);
                }
                echo $replycontent;
            } elseif ($topic_topics['post_status'] == 0) {
                echo '内容被隐藏';
            }
            echo '<br/>';
            //回复回帖

            //隐藏回帖，恢复回帖
            if (isset($_SESSION['rank'])) {
                if ($_SESSION['rank'] == 10 || $auth_edit == $_SESSION['rank']) {
                    echo '<form>';
                    echo '<input type="button" onclick="ajaxpost_edit_replyhide(' . $topic_topics['post_id'] . ')" value="改变状态"/>';
                    echo '</form>';
                }
                if ($_SESSION['rank'] == 10 || $_SESSION['rank'] == $result_array['user_uid']) {
                    ?>
                    <a href="topicediter.php?action=edittopic&fid=<?= $fid ?>&tid=<?= $topicid ?>&back=<?= base64_encode($_SERVER['REQUEST_URI']) ?>">编辑贴子</a>
                    <?php
                }
            }
            $count_like = count(explode(",", $topics['topic_like']));
            $randLike = rand(1, 2);
            if (isset($_SESSION['uid']) && $_SESSION['uid'] != $topics['topic_poster']) {
                echo '<br/><img id="likepng" src="img/like' . $randLike . '.png" onclick="ajax_addLike(' . $topics['topic_id'] . ')" width="40" height="38"></img><span id="like">' . ($count_like - 1) . '</span>';
            } elseif (isset($_SESSION['uid']) && $_SESSION['uid'] == $topics['topic_poster']) {
                echo '<br/><img id="likepng" onclick="ulike()" src="img/like' . $randLike . '.png" width="40" height="38"></img><span id="like">' . ($count_like - 1) . '</span>';
            } else {
                echo '<br/><img id="likepng" onclick="olike(\'' . base64_encode($url) . '\')" src="img/like' . $randLike . '.png" width="40" height="38"></img><span id="like">' . ($count_like - 1) . '</span>';
            }
            /**
             * 回帖
             */
            //查询回复
            //输入topicid和p 查出回复信息
            $result_array = sql_topicid_posts_asc($gettopicid, 1, 0, (10 - 1));
            if ($result_array != null) {
                if ($p == 1) {
                    $floor = 2;
                }
                while (($result = mysqli_fetch_array($result_array)) != null) {
                    //回帖用户：
                    $replyuid = $result['post_authuid'];
                    $replyposter = sql_search_uid_all($replyuid);
                    $replyname = $replyposter['user_username'];
                    $replyhimg = $replyposter['user_himg'];
                    $replylevel = $replyposter['user_level'];
                    $replytime = date("Y年m月d日 H:i:s", $result['post_createtime']);
                    $replycontent = $result['post_content'];
                    $replyid = $result['post_id'];
                    echo '<tr>';
                    ?>
                    <td id="reply<?= $replyid ?>">
                        <center>
                            <a href="home.php?mod=space&uid=<?= $replyuid ?>"><?= $replyname ?></a>
                            <br/>
                            <img src="<?= $replyhimg ?>"/><br/>
                            <?php
                            echo '等级：' . $replylevel . '<br />';
                            $result1 = sql_search_medals($replyposter['user_uid']);
                            while (($result1_array = mysqli_fetch_array($result1)) != null) {
                                $narray = sql_medalid_medals($result1_array['medal_id']);
                                echo '<img src="' . $narray['medal_url'] . '" width="50px"
                                       height="50px" title="' . $narray['medal_name'] . '"/>';
                            }
                            echo '<br />用户组：' . $user_rankname = sql_search_rank_rankname($replyposter['user_rank']);
                            ?>
                        </center>
                    </td>
                    <td>
                    <?php
                    if ($replyuid == $posteruid) {
                        echo '<font color="#ff0000">楼主</font>';
                    }
                    ?>
                    发表于：<?= $replytime ?>&nbsp;
                    <?php
                    echo ffloor($floor);
                    $floor++;
                    ?>
                    <br/>
                    <?php
                    if ($result['post_status'] == 1) {
                        $query = sql_key_query();
                        while (($array = mysqli_fetch_array($query)) != false) {
                            $replycontent = preg_replace('/' . $array['key_name'] . '/', $array['key_change'], $replycontent);
                        }
                        echo $replycontent;
                        ?>
                        <div>
                            <?php
                            //楼中楼翻页：gg
                            $pp = "lzlp" . $replyid;
                            $lzlp = isset($_GET[$pp]) ? $_GET[$pp] : 1;


                            $count = sql_lzl_count($replyid);
                            $pagenum_ = ceil($count / 5);


                            $lzlresut = sql_postid_lzl($replyid, 2, (($lzlp - 1) * 5), 5);


                            echo '<p style="text-align: left">';
                            if ($lzlp > 1) {
                                echo '<a href="topic.php?topicid=' . $gettopicid . '&p=' . $p . '&' . $pp . '=' . ($lzlp - 1) . '#reply' . $replyid . '"><<上一页</a>&nbsp;&nbsp;';
                            }
                            echo '共 ', $count, ' 条';
                            if ($pagenum_ > 1) {
                                for ($i = 1; $i <= $pagenum_; $i++) {
                                    if ($i == $lzlp) {
                                        echo '&nbsp;[', $i, ']';
                                    } else {
                                        echo '&nbsp;<a href="topic.php?topicid=' . $gettopicid . '&p=' . $p . '&' . $pp . '=' . $i . '#reply' . $replyid . '">' . $i . '</a>';
                                    }
                                }
                            }
                            if ($lzlp < $pagenum_) {
                                echo '&nbsp;&nbsp;<a href="topic.php?topicid=' . $gettopicid . '&p=' . $p . '&' . $pp . '=' . ($lzlp + 1) . '#reply' . $replyid . '">下一页>></a>';
                            }
                            echo '</p>';


                            echo '<hr/>';
                            while (($lzl_array = mysqli_fetch_array($lzlresut)) != false) {
                                ?>
                                <a href="home.php?mod=space&uid=<?= $replyuid ?>"
                                   target="_blank"><?= sql_searchName_login_uid_name($lzl_array['post_authuid']) ?></a>：<?= htmlspecialchars($lzl_array['post_content']) ?>
                                <?php
                                if (isset($_SESSION['uid'])) {
                                    ?>
                                    <a href='#reply<?= $replyid ?>'
                                       onclick="getElementById('addLzl<?= $replyid ?>').innerHTML=add_lzl_post(<?= $topicid ?>, <?= $replyid ?>, <?= $_SESSION['uid'] ?>, '<?= $replyname ?>')">回复</a>
                                    <?php
                                }
                                if (isset($_SESSION['rank']) && $_SESSION['rank'] == 10 || isset($_SESSION['rank']) && $_SESSION['rank'] == $auth_edit|| isset($_SESSION['uid']) && ($_SESSION['uid'] == $lzl_array['post_authuid'])) {
                                    ?>
                                    <input type="button" onclick="ajaxpost_edit_replydel(<?= $lzl_array['post_id'] ?>)"
                                           value="删除"/>
                                    <?php
                                }
                                ?>
                                <hr/>
                                <?php
                            }
                            ?>
                        </div>
                        <div id="addLzl<?= $replyid ?>"></div>
                        <?php
                    } elseif ($result['post_status'] == 0) {
                        echo '回贴被隐藏';
                    }
                    echo '<br/>';
                    //回复回帖

                    //隐藏回帖，恢复回帖
                    if (isset($_SESSION['rank'])) {
                        if ($_SESSION['rank'] == 10 || $auth_edit == $_SESSION['rank']) {
                            echo '<input type="button" onclick="ajaxpost_edit_replyhide(' . $replyid . ')" value="改变状态"/>';
                            echo '<input type="button" onclick="ajaxpost_edit_replydel(' . $replyid . ')" value="删除"/>';
                        }elseif ($_SESSION['uid'] == $replyuid){
	                        echo '<input type="button" onclick="ajaxpost_edit_replydel(' . $replyid . ')" value="删除"/>';
                        }
                    }
                    if (isset($_SESSION['uid']) && $_SESSION['uid'] != $replyuid) {
                        $text = "回复 " . $replyname . " ：" . htmlspecialchars_decode($user_all['user_isignatur']);
                        if (isset($_SESSION['uid']))
                            echo '<a href="#replycontent"><input type="button" onclick="ue.setContent(\'' . $text . '\');" value="回复"/></a>';
                    }
                    //楼中楼回复
                    if ($result['post_status'] != 0 && isset($_SESSION['uid'])) {
                        ?>
                        <a href="#reply<?= $replyid ?>"
                           onclick="getElementById('addLzl<?= $replyid ?>').innerHTML=add_lzl_post(<?= $topicid ?>,<?= $replyid ?>, <?= $_SESSION['uid'] ?>); document.getElementById('lzl<?= $replyid ?>').focus();">楼中楼回复</a>

                        </td>
                        <?php
                    }
                    echo '</tr>';
                }
            }
        } elseif ($p > 1) {
            /**
             * 回帖
             */
            $floor = ($p - 1) * 10 + 1;
            //查询回复 p2+
            //输入topicid和p 查出回复信息
            $result_array = sql_topicid_posts_asc($gettopicid, 1, (10 * ($p - 1) - 1), 10);
            if ($result_array != null) {
                while (($result = mysqli_fetch_array($result_array)) != null) {
                    //回帖用户：
                    $replyuid = $result['post_authuid'];
                    $replyposter = sql_search_uid_all($replyuid);
                    $replyname = $replyposter['user_username'];
                    $replyhimg = $replyposter['user_himg'];
                    $replylevel = $replyposter['user_level'];
                    $replytime = date("Y年m月d日 H:i:s", $result['post_createtime']);
                    $replycontent = $result['post_content'];
                    $replyid = $result['post_id'];
                    echo '<tr>';
                    ?>
                    <td id="reply<?= $replyid ?>">
                        <center>
                            <a href="home.php?mod=space&uid=<?= $replyuid ?>" target="_blank"><?= $replyname ?></a>
                            <br/>
                            <img src="<?= $replyhimg ?>"/><br/>
                            <?php
                            echo '等级：' . $replylevel . '<br />';
                            $result1 = sql_search_medals($replyposter['user_uid']);
                            while (($result1_array = mysqli_fetch_array($result1)) != null) {
                                $narray = sql_medalid_medals($result1_array['medal_id']);
                                echo '<img src="' . $narray['medal_url'] . '" width="50px"
                                       height="50px" title="' . $narray['medal_name'] . '"/>';
                            }
                            echo '<br />用户组：' . $user_rankname = sql_search_rank_rankname($replyposter['user_rank']);
                            ?>
                        </center>
                    </td>
                    <td>
                    <?php
                    if ($replyuid == $posteruid) {
                        echo '楼主';
                    }
                    ?>
                    发表于：<?= $replytime ?>
                    <?php
                    echo ffloor($floor);
                    $floor++;
                    ?>
                    <br/>
                    <?php
                    if ($result['post_status'] == 1) {
                        $query = sql_key_query();
                        while (($array = mysqli_fetch_array($query)) != false) {
                            $replycontent = preg_replace('/' . $array['key_name'] . '/', $array['key_change'], $replycontent);
                        }
                        echo $replycontent;
                        ?>
                        <div>
                            <?php
                            //楼中楼翻页：没法做
                            $lzlresut = sql_postid_lzl($replyid, 2, 0, 100);
                            echo '<hr/>';
                            while (($lzl_array = mysqli_fetch_array($lzlresut)) != false) {
                                ?>
                                <a href="home.php?mod=space&uid=<?= $replyuid ?>"
                                   target="_blank"><?= sql_searchName_login_uid_name($lzl_array['post_authuid']) ?></a>：<?= htmlspecialchars($lzl_array['post_content']) ?>
	                            <?php
	                            if (isset($_SESSION['uid'])) {
		                            ?>
                                    <a href='#reply<?= $replyid ?>'
                                       onclick="getElementById('addLzl<?= $replyid ?>').innerHTML=add_lzl_post(<?= $topicid ?>, <?= $replyid ?>, <?= $_SESSION['uid'] ?>, '<?= $replyname ?>')">回复</a>
		                            <?php
	                            }
	                            if (isset($_SESSION['rank']) && $_SESSION['rank'] == 10 || isset($_SESSION['rank']) && $_SESSION['rank'] == $auth_edit|| isset($_SESSION['uid']) && ($_SESSION['uid'] == $lzl_array['post_authuid'])) {
		                            ?>
                                    <input type="button" onclick="ajaxpost_edit_replydel(<?= $lzl_array['post_id'] ?>)"
                                           value="删除"/>
		                            <?php
	                            }
	                            ?>
                                <hr/>
	                            <?php
                            }
                            ?>
                        </div>
                        </div>
                        <div id="addLzl<?= $replyid ?>"></div>
                        <?php
                    } elseif ($result['post_status'] == 0) {
                        echo '回贴被隐藏';
                    }
                    echo '<br/>';
                    //回复回帖

                    //隐藏回帖，恢复回帖
	                if (isset($_SESSION['rank'])) {
		                if ($_SESSION['rank'] == 10 || $auth_edit == $_SESSION['rank']) {
			                echo '<input type="button" onclick="ajaxpost_edit_replyhide(' . $replyid . ')" value="改变状态"/>';
			                echo '<input type="button" onclick="ajaxpost_edit_replydel(' . $replyid . ')" value="删除"/>';
		                }elseif ($_SESSION['uid'] == $replyuid){
			                echo '<input type="button" onclick="ajaxpost_edit_replydel(' . $replyid . ')" value="删除"/>';
		                }
	                }
	                if (isset($_SESSION['uid']) && $_SESSION['uid'] != $replyuid) {
		                $text = "回复 " . $replyname . " ：" . htmlspecialchars_decode($user_all['user_isignatur']);
		                if (isset($_SESSION['uid']))
			                echo '<a href="#replycontent"><input type="button" onclick="ue.setContent(\'' . $text . '\');" value="回复"/></a>';
	                }
                    if ($result['post_status'] != 0 && isset($_SESSION['uid'])) {
                        ?>
                        <a href="#reply<?= $replyid ?>"
                           onclick="getElementById('addLzl<?= $replyid ?>').innerHTML=add_lzl_post(<?= $topicid ?>,<?= $replyid ?>, <?= $_SESSION['uid'] ?>); document.getElementById('lzl<?= $replyid ?>').focus();">楼中楼回复</a>

                        </td>
                        <?php
                    }
                    echo '</tr>';
                }
            }
        }
        ?>

    </table>
    <br/>

<?php
echo '<p style="text-align: right">';
if ($p > 1) {
    echo '<a href="topic.php?topicid=' . $gettopicid . '&p=' . ($p - 1) . '"><<上一页</a>&nbsp;&nbsp;';
}
echo '共 ', $count_array['COUNT(*)'], ' 条';
if ($pagenum > 1) {
    for ($i = 1; $i <= $pagenum; $i++) {
        if ($i == $p) {
            echo '&nbsp;[', $i, ']';
        } else {
            echo '&nbsp;<a href="topic.php?topicid=' . $gettopicid . '&p=' . $i . '">' . $i . '</a>';
        }
    }
}
if ($p < $pagenum) {
    echo '&nbsp;&nbsp;<a href="topic.php?topicid=' . $gettopicid . '&p=' . ($p + 1) . '">下一页>></a>';
}
echo '</p>';
?>
    <br/>
    <a href="forums.php?forumid=<?= $topics['topic_forumid'] ?>&p=1"">返回列表</a>
    <hr>
<?php
if (isset($_SESSION['uid'])) {
    $user_all = sql_search_uid_all($_SESSION['uid']);
    ?>

    <h5>回复</h5>
    <div id="replyhead">
        <center>
            <img src="<?= $loginers['user_himg'] ?>"/>
        </center>
    </div>
    <!-- 发表回复区 -->
    <div id="replycontent">
        <!-- 加载编辑器的容器 -->
        <script id="container" name="content" type="text/plain">
            <p></p><?= htmlspecialchars_decode($user_all['user_isignatur']) ?></script>
        <!-- 配置文件 -->
        <script type="text/javascript" src="utf8-php/ueditor.config.js"></script>
        <!-- 编辑器源码文件 -->
        <script type="text/javascript" src="utf8-php/ueditor.all.js"></script>
        <!-- 实例化编辑器 -->
        <script type="text/javascript">
            var ue = UE.getEditor('container');
        </script>
        <!-- 隐藏的post -->
        <input type="hidden" id="page" value="<?= $p ?>"/>
        <input type="hidden" id="topicid" value="<?= $_GET['topicid'] ?>"/>
        <br/>
        <button type="button" onclick="ajaxpost_replytopic(<?= $_SESSION['uid'] ?>)">发表回复</button>
    </div>
    <?php
}

//require 'core/footer.php';
?>